package com.easy.frame.servers.sys.controller;

import com.easy.frame.base.api.pojo.PageDTO;
import com.easy.frame.base.api.pojo.PageVO;
import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.service.base.BaseController;
import com.easy.frame.servers.sys.dto.SysMsgTemplatePageDTO;
import com.easy.frame.servers.sys.entity.SysMsgTemplate;
import com.easy.frame.servers.sys.service.SysMsgTemplateService;
import com.easy.frame.servers.sys.vo.SysMsgTemplatePageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 2021/1/3
 * @author msp
 */
@Api(tags = "消息模板/sys-msg-template")
@RestController
@RequestMapping("/sys-msg-template")
public class SysMsgTemplateController extends BaseController {

    @Resource
    private SysMsgTemplateService service;

    @ApiOperation("分页")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysMsgTemplatePageVO>> page(@RequestBody PageDTO<SysMsgTemplatePageDTO> pageDTO) {
        PageVO<SysMsgTemplatePageVO> pageVO = service.page(pageDTO);
        return success(pageVO);
    }

    @ApiOperation("保存")
    @PostMapping
    public ServiceResponse<?> save(@RequestBody SysMsgTemplate sysMsgTemplate) {
        if (service.checkCode(sysMsgTemplate.getTenantId(), sysMsgTemplate.getCode())) {
            service.save(sysMsgTemplate);
            return success(sysMsgTemplate);
        } else {
            return fail("编号已存在，请确认后重新添加");
        }
    }

    @ApiOperation("编辑修改")
    @PutMapping
    public ServiceResponse<SysMsgTemplate> update(@RequestBody SysMsgTemplate entity) {
        service.updateEntityById(entity);
        return success(entity);
    }

    @ApiOperation("by-id")
    @DeleteMapping("/by-id")
    public ServiceResponse<?> deleteById(@ApiParam(value = "主键id", required = true)
                                         @RequestParam Long id) {
        SysMsgTemplate temp = service.getById(id);
        if (temp == null) {
            return fail("删除失败，无效数据。");
        } else {
            service.deleteById(id);
            return success();
        }
    }
}
